using LiteDB;

namespace PHMEE.VibrationDataGenerator.Data;

public class TaskConfigRepository
{
    private readonly LiteDatabase _database;
    private readonly ILiteCollection<TaskConfig> _taskConfigsCollection;

    public TaskConfigRepository()
    {
        _database = new LiteDatabase(@"Filename=PhmVibGenerator.db; Connection=shared;");
        _taskConfigsCollection = _database.GetCollection<TaskConfig>("taskConfigs");
    }

    public List<TaskConfig> GetAllTasks()
    {
        return _taskConfigsCollection.FindAll()
            .OrderByDescending(t => t.CreatedTime)
            .ToList();
    }

    public TaskConfig GetTaskById(ObjectId id)
    {
        return _taskConfigsCollection.FindById(id);
    }

    public void AddTask(TaskConfig taskConfig)
    {
        taskConfig.CreatedTime = DateTime.UtcNow;
        _taskConfigsCollection.Insert(taskConfig);
    }
    
    public void UpdateTask(TaskConfig taskConfig)
    {
        _taskConfigsCollection.Update(taskConfig);
    }

    public void DeleteTask(ObjectId id)
    {
        _taskConfigsCollection.Delete(id);
    }
}